#include<bits/stdc++.h>
using namespace std;
const int con=2010;
int t,n,a[200005],dp[con][con],dp2[200005][118],gt1,gt2,bb;
void work()
{
	int ans=0;
	memset(dp,0,sizeof(dp));
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<n;++j)
		{
			//cout<<"!"<<i<<j<<endl;
			if(i==j)continue;
			if(i>j)
			{
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]+(a[i+1]==a[i]?a[i]:0));
				dp[i][i+1]=max(dp[i][i+1],dp[i][j]+(a[i+1]==a[j]?a[j]:0));
			}
			else
			{
				dp[i][j+1]=max(dp[i][j+1],dp[i][j]+(a[j+1]==a[j]?a[j]:0));
				dp[j+1][j]=max(dp[j+1][j],dp[i][j]+(a[j+1]==a[i]?a[i]:0));
			}
		}
	}
	for(int i=0;i<n;++i) ans=max(ans,dp[i][n]);
	for(int i=0;i<n;++i) ans=max(ans,dp[n][i]);
	cout<<ans<<endl;
	bb=ans;
}
int ch1(int x,int y)
{
	return(x)*11+y;
}
/*void ch2(int num)
{
	gt1=num%11;
	gt2=num/11;
}*/
void work2()
{
	int ans=0;
	memset(dp2,0x3f,sizeof(dp2));
	dp2[1][ch1(a[1],0)]=0;
	dp2[1][ch1(0,a[1])]=0;
	//dp2[2][ch1(a[1],a[2])]=0;
	for(int s=1;s<=n;++s)
	{
		for(int i=0;i<=10;++i)
		{
			for(int j=0;j<=10;++j)
			{
				int ch=ch1(i,j);
				if(dp2[s][ch]==0x3f3f3f3f) continue;
				if(dp2[s+1][ch1(i,a[s+1])]==0x3f3f3f3f)
					dp2[s+1][ch1(i,a[s+1])]=dp2[s][ch]+(a[s+1]==j?j:0);
				else dp2[s+1][ch1(i,a[s+1])]=max(dp2[s+1][ch1(i,a[s+1])],
										dp2[s][ch]+(a[s+1]==j?j:0));
				if(dp2[s+1][ch1(a[s+1],j)]==0x3f3f3f3f)
					dp2[s+1][ch1(a[s+1],j)]=dp2[s][ch]+(a[s+1]==i?i:0);
				else dp2[s+1][ch1(a[s+1],j)]=max(dp2[s+1][ch1(a[s+1],j)],
										dp2[s][ch]+(a[s+1]==i?i:0));
			}
		}
	}
	for(int i=0;i<101;++i) if(dp2[n][i]!=0x3f3f3f3f) ans=max(ans,dp2[n][i]);
	cout<<ans<<endl;
	//cout<<bb<<" "<<ans<<endl;
	/*if(ans!=bb)
	{
		for(int i=1;i<=n;++i)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}*/
}
int main(){
	ios::sync_with_stdio(false);
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;++i)
		{
			cin>>a[i];
		}
		if(n<con)
		work();
		else 
		work2();
	}
	return 0;
}
